package com.senior.completable;

import com.yu.threadpool.ThreadPoolUntils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CompletableFutureDemo {
    private static ThreadPoolExecutor threadPoolExecutor;

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        threadPoolExecutor = ThreadPoolUntils.getThreadPool();
        Integer result = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int i = 10/0;
            return 1;
        }).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("result:" + v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            return null;
        }).join(); //进行阻塞，等待上面算法完成 join() 和 get()  一个效果，只是少抛一个异常

        System.out.println("main thread over");
        TimeUnit.SECONDS.sleep(5);
    }
    
}
